/*
 Copyright 2009 Kaspars Dancis
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
 http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

#import "AppController.h"
#import "GameConfig.h"
#import "ReplaceLayerAction.h"
#import "SimpleAudioEngine.h"

#define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)

@implementation AppController

@synthesize textFieldArray,scrollView,nPlayerNum,nBottle_PlayerNum,window;

- (void) removeStartupFlicker
{
	//
	// THIS CODE REMOVES THE STARTUP FLICKER
	//
	// Uncomment the following code if you Application only supports landscape mode
	//
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
	
	//	CC_ENABLE_DEFAULT_GL_STATES();
	//	CCDirector *director = [CCDirector sharedDirector];
	//	CGSize size = [director winSize];
	//	CCSprite *sprite = [CCSprite spriteWithFile:@"Default.png"];
	//	sprite.position = ccp(size.width/2, size.height/2);
	//	sprite.rotation = -90;
	//	[sprite visit];
	//	[[director openGLView] swapBuffers];
	//	CC_ENABLE_DEFAULT_GL_STATES();
	
#endif // GAME_AUTOROTATION == kGameAutorotationUIViewController	
}

- (void) applicationDidFinishLaunching:(UIApplication*)application
{
	application.idleTimerDisabled = YES; // we don't want the screen to sleep during our game 
	
	window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	
	[window setMultipleTouchEnabled:YES];
	
	CCLOG(@"window[%f][%f]",window.frame.size.width,window.frame.size.height);
	
	// Try to use CADisplayLink director
	// if it fails (SDK < 3.1) use the default director
	if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
		[CCDirector setDirectorType:kCCDirectorTypeDefault];
	
	CCDirector *director = [CCDirector sharedDirector];
	
	// Init the View Controller
	viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
	viewController.wantsFullScreenLayout = YES;
	
	//
	// Create the EAGLView manually
	//  1. Create a RGB565 format. Alternative: RGBA8
	//	2. depth format of 0 bit. Use 16 or 24 bit for 3d effects, like CCPageTurnTransition
	//
	//
	EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
								   pixelFormat:kEAGLColorFormatRGB565	// kEAGLColorFormatRGBA8
								   depthFormat:0						// GL_DEPTH_COMPONENT16_OES
						];
	
	// attach the openglView to the director
	[director setOpenGLView:glView];
	
	self.nPlayerNum = 1; //默认为1
	
	self.nBottle_PlayerNum = 8;
	
	[self initTextField];
	
	//	// Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
	//	if( ! [director enableRetinaDisplay:YES] )
	//		CCLOG(@"Retina Display Not supported");
	
	//
	// VERY IMPORTANT:
	// If the rotation is going to be controlled by a UIViewController
	// then the device orientation should be "Portrait".
	//
	// IMPORTANT:
	// By default, this template only supports Landscape orientations.
	// Edit the RootViewController.m file to edit the supported orientations.
	//
	
//#if GAME_AUTOROTATION == kGameAutorotationUIViewController
	[director setDeviceOrientation:kCCDeviceOrientationPortrait];
//#else
//	[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
//#endif
	
	[director setAnimationInterval:1.0/60];
	[director setDisplayFPS:NO];
	
	if(isRetina)
	{
		//[director enableRetinaDisplay:YES];
	}
	else 
	{
		;
	}

	
	// make the OpenGLView a child of the view controller
	[viewController setView:glView];
	
	// make the View Controller a child of the main window
	[window addSubview: viewController.view];
	
	[window makeKeyAndVisible];
	
	// Default texture format for PNG/BMP/TIFF/JPEG/GIF images
	// It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
	// You can change anytime.
	[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
	
	
	// Removes the startup flicker
	[self removeStartupFlicker];
	
	// add layer
	scene = [CCScene node];
	
	/*
	m_pBg = [[CCSprite alloc] init];
	[m_pBg initWithFile:@"bg_card.png"];
	m_pBg.anchorPoint = ccp(0.0f,0.0f);
	[scene addChild:m_pBg z:0];
	*/
	
	currentLayer = nil;
	
	[CCMenuItemFont setFontSize:40];
	
	//[PASoundMgr sharedSoundManager];
	
	[[CCDirector sharedDirector] runWithScene: scene];
	
	gameState = StateNone;
	
	[[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:@"bankground_bottle.mp3"];
	[[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:@"bankground_card.mp3"];
	
	[self TurnToHomePage];
}



- (void) dealloc
{
	[textFieldArray release];
	self.scrollView = nil;
	
	[[CCDirector sharedDirector] release];
	
	[m_pBg release];
	
	[viewController release];
	[window		    release];
	
	[super dealloc];
}



// getting a call, pause the game
-(void) applicationWillResignActive:(UIApplication *)application
{
	[[CCDirector sharedDirector] pause];
}



// call got rejected
-(void) applicationDidBecomeActive:(UIApplication *)application
{
	[[CCDirector sharedDirector] resume];
}

// purge memroy
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application 
{
	[[CCDirector sharedDirector] purgeCachedData];
}

// next delta time will be zero
-(void) applicationSignificantTimeChange:(UIApplication *)application
{
	[[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
}

-(void) applicationDidEnterBackground:(UIApplication*)application 
{
	[[CCDirector sharedDirector] stopAnimation];
}

-(void) applicationWillEnterForeground:(UIApplication*)application 
{
	[[CCDirector sharedDirector] startAnimation];
}

- (void)applicationWillTerminate:(UIApplication *)application 
{
	CCDirector *director = [CCDirector sharedDirector];
	
	[[director openGLView] removeFromSuperview];
	
	[viewController release];
	
	[window release];
	
	[director end];	
}

/*
-(void)enterName 
{
	EnterName *l = [[[EnterName alloc] initWithDelegate:self window:window] autorelease];	
	ReplaceLayerAction *replaceScreen = [[[ReplaceLayerAction alloc] initWithScene:scene layer:l replaceLayer:currentLayer] autorelease];
	[scene runAction: replaceScreen];
	currentLayer = l;
}
*/

-(void)setTextFieldArrayVisable:(BOOL)isHidden
{
	scrollView.hidden = isHidden;
	
	rightArrow.hidden = isHidden;
	leftArrow.hidden  = isHidden;
	
	for (UIView *pView in textFieldArray)
	{
		[pView setHidden:isHidden];
	}
	
}

-(void)AddTextField
{
	UITextField *pTextField = [[UITextField alloc] init];
	
	[pTextField setFrame:CGRectMake(50, 10 + (nPlayerNum - 1 ) * TEXTFIELD_STEP, 221, 55)];
	
	UIImage * pImage = [UIImage imageNamed:@"name_input.png"];

	//[pTextField setTextColor:[UIColor colorWithRed:96.0f green:121.0f blue:143.0f alpha:1.0f]];
	
	//[pTextField setTextColor:[UIColor grayColor]];
	
	[pTextField setTextColor:[UIColor colorWithRed:0x60 / 255.0 green:0x79 / 255.0 blue:0x8f / 255.0 alpha:1.0]];
	
	//pTextField.font = [UIFont fontWithName:@"Helvetica-BoldOblique" size:24];
	
	pTextField.font = [UIFont fontWithName:@"Helvetica-Bold" size:24];
	
	pTextField.placeholder = [NSString stringWithFormat:@"点击输入姓名",nPlayerNum];
	
	UILabel * pLab = [[UILabel alloc] initWithFrame:CGRectMake(20, 16, 20, 24)];
	pLab.font      = [UIFont fontWithName:@"Helvetica-BoldOblique" size:24];
	pLab.textColor = [UIColor colorWithRed:0x60 / 255.0 green:0x79 / 255.0 blue:0x8f / 255.0 alpha:1.0];
	pLab.text      = [NSString stringWithFormat:@"%d",nPlayerNum];
	pLab.backgroundColor = [UIColor clearColor];
	
	[pTextField addSubview:pLab];
	
	[pLab release];
	
	[pTextField setTextAlignment:UITextAlignmentCenter];
	[pTextField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
	[pTextField setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
	[pTextField setClearsOnBeginEditing:YES];
	
	[pTextField setBorderStyle:UITextBorderStyleNone];
	
	[pTextField setDelegate:self];
	[pTextField setReturnKeyType:UIReturnKeyDone];
	[pTextField setAutocapitalizationType:UITextAutocapitalizationTypeWords];
										  
	pTextField.background = pImage;
	
	pTextField.tag = 10000 + nPlayerNum - 1;
	
	[scrollView addSubview:pTextField];
	
	scrollView.contentSize   = CGSizeMake(SCREEN_WIDTH, scrollView.contentSize.height + TEXTFIELD_STEP);
	
	if(scrollView.contentSize.height > SCROLL_HEIGHT)
	{
		scrollView.contentOffset = CGPointMake(0,scrollView.contentOffset.y + TEXTFIELD_STEP);
	}
	
	//[pTextField setHidden:YES];
	
	[textFieldArray addObject:pTextField];
	
	[pTextField release];
	
	//调整BUTTON
	[leftArrow  setFrame:CGRectMake(leftArrow.frame.origin.x, leftArrow.frame.origin.y + TEXTFIELD_STEP, leftArrow.frame.size.width, leftArrow.frame.size.height) ];
	[rightArrow setFrame:CGRectMake(rightArrow.frame.origin.x,rightArrow.frame.origin.y + TEXTFIELD_STEP,rightArrow.frame.size.width,rightArrow.frame.size.height)];
	
}

-(void)RemoveTextField
{
	UITextField * pView = (UITextField *)[scrollView viewWithTag:10000 + nPlayerNum];
	if(pView)
	{
		[pView removeFromSuperview];
		
		scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, scrollView.contentSize.height - TEXTFIELD_STEP);
		
		[textFieldArray removeObject:pView];
	}
	
	//调整BUTTON
	[leftArrow  setFrame:CGRectMake(leftArrow.frame.origin.x, leftArrow.frame.origin.y  - TEXTFIELD_STEP, leftArrow.frame.size.width, leftArrow.frame.size.height) ];
	[rightArrow setFrame:CGRectMake(rightArrow.frame.origin.x,rightArrow.frame.origin.y - TEXTFIELD_STEP,rightArrow.frame.size.width,rightArrow.frame.size.height)];
	
	
}

-(void)addPressed:(id)sender
{
	[self TipSound];
	
	if (nPlayerNum < 8)
	{
		nPlayerNum ++;

		NSString * ss = [NSString stringWithFormat:@"%d" , CARD_PLAYER_ARROW_RIGHT];
		
		[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangNum" 
															object:ss];
		
		[self AddTextField];
	}
}

-(void)removePressed:(id)sender
{
	[self TipSound];
	
	if (nPlayerNum > 1)
	{
		nPlayerNum --;
		
		NSString * ss = [NSString stringWithFormat:@"%d" , CARD_PLAYER_ARROW_LEFT];
		
		[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangNum" 
															object:ss];
		
		[self RemoveTextField];
	}
}

-(void)initTextField
{
	if (!textFieldArray)
	{
		textFieldArray = [[NSMutableArray alloc] initWithCapacity:0];
	}
	
	int height = 10;
	
	if (!scrollView)
	{

		scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 80, SCREEN_WIDTH, SCROLL_HEIGHT)];

		//scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, height, SCREEN_WIDTH, SCREEN_HEIGHT/2-30)];
		//scrollView.delegate = self;
		//scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);

		scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, height);
		
		[[[CCDirector sharedDirector] openGLView] addSubview: scrollView];
		
		scrollView.hidden = YES;
	}
	
	
	//int nstep  = 75;
	
	for (int i = 0; i < nPlayerNum; i++)
	{
		UITextField *pTextField = [[UITextField alloc] init];
		
		[pTextField setFrame:CGRectMake(50, height + i * TEXTFIELD_STEP, 221, 55)];
		
		UIImage * pImage = [UIImage imageNamed:@"name_input.png"];
		
		[pTextField setTextColor:[UIColor colorWithRed:0x60 / 255.0 green:0x79 / 255.0 blue:0x8f / 255.0 alpha:1.0]];
		
		pTextField.font = [UIFont fontWithName:@"Helvetica-Bold" size:24];
		
		pTextField.placeholder = [NSString stringWithFormat:@"点击输入姓名"];
		
		UILabel * pLab = [[UILabel alloc] initWithFrame:CGRectMake(20, 16, 20, 24)];
		pLab.font      = [UIFont fontWithName:@"Helvetica-BoldOblique" size:24];
		pLab.textColor = [UIColor colorWithRed:0x60 / 255.0 green:0x79 / 255.0 blue:0x8f / 255.0 alpha:1.0];
		pLab.text      = [NSString stringWithFormat:@"%d",i + 1];
		pLab.backgroundColor = [UIColor clearColor];
		
		[pTextField addSubview:pLab];
		
		[pLab release];
		
		//[pTextField setTextColor:[UIColor grayColor]];
		
		//[pTextField setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"input_name.png"]]];
		[pTextField setTextAlignment:UITextAlignmentCenter];
		[pTextField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
		[pTextField setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
		[pTextField setClearsOnBeginEditing:YES];
		
		[pTextField setBorderStyle:UITextBorderStyleNone];
		
		[pTextField setDelegate:self];
		[pTextField setReturnKeyType:UIReturnKeyDone];
		[pTextField setAutocapitalizationType:UITextAutocapitalizationTypeWords];
		
		pTextField.background = pImage;
		
		pTextField.tag = 10000 + i;
		
		[scrollView addSubview:pTextField];
		
		scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, scrollView.contentSize.height + TEXTFIELD_STEP);
		
		[pTextField setHidden:YES];
		
		[textFieldArray addObject:pTextField];
		
		[pTextField release];
	}

	//增加人数按钮
	UIImage * addImage = [UIImage imageNamed:@"add.png"];

	rightArrow = [[UIButton alloc] initWithFrame:CGRectZero];
	
	[rightArrow setHidden:YES];
	
	[rightArrow setBackgroundImage:addImage forState:UIControlStateNormal];
	
	[rightArrow setFrame:CGRectMake(SCREEN_WIDTH/2 - 45 - 20,scrollView.contentSize.height + 5,addImage.size.width,addImage.size.height)];
	
	[rightArrow setBackgroundColor:[UIColor clearColor]];
	[rightArrow addTarget:self action:@selector(addPressed:)
		 forControlEvents:UIControlEventTouchUpInside];
	
	[scrollView addSubview:rightArrow];
	
	//减少人数按钮
	UIImage * removeImage = [UIImage imageNamed:@"remove.png"];
	
	leftArrow = [[UIButton alloc] initWithFrame:CGRectZero];
	
	[leftArrow setHidden:YES];
	
	[leftArrow setBackgroundImage:removeImage forState:UIControlStateNormal];
	
	[leftArrow setFrame:CGRectMake(SCREEN_WIDTH/2 + 20,scrollView.contentSize.height + 5,removeImage.size.width,removeImage.size.height)];
	
	[leftArrow setBackgroundColor:[UIColor clearColor]];
	[leftArrow addTarget:self action:@selector(removePressed:)
		 forControlEvents:UIControlEventTouchUpInside];
	
	[scrollView addSubview:leftArrow];
	
	scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, scrollView.contentSize.height + 5 + removeImage.size.height);
	
}

#pragma mark UITextField
- (BOOL)textFieldShouldReturn:(UITextField*)textField 
{
	//Terminate editing 
	[textField resignFirstResponder];
	
	[scrollView setContentOffset:oldContentOff];
	
	return YES; 
} 

-(void)textFieldDidBeginEditing:(UITextField *)textField  
{  
	oldContentOff = scrollView.contentOffset;
	
	float p = textField.frame.origin.y - scrollView.contentOffset.y;
	
	NSLog(@"[%f]",p);
	
	if( (textField.frame.origin.y - scrollView.contentOffset.y) > (185) )
	{
		[scrollView setContentOffset:CGPointMake(oldContentOff.x,oldContentOff.y + (textField.frame.origin.y - scrollView.contentOffset.y) - (185) + 66)];
	}
	/*
    //activeField = textField;
	switch (textField.tag) 
	{
		case 10000:
			break;
		case 10001:
			break;
		case 10002:
		{
			
		}
			break;
		case 10003:
		{
			[scrollView setContentOffset:CGPointMake(oldContentOff.x,oldContentOff.y + 100)];
		}
			break;
		case 10004:
		{
			
		}
			break;
		case 10005:
		{
			
		}
			break;
		case 10006:
		{
			
		}
			break;
		case 10007:
		{
			
		}
			break;
		default:
			break;
	}
	*/
}

- (void)textFieldDidEndEditing:(UITextField*)textField 
{ 
	NSLog(@"textFieldDidEndEditing");
	
	for (UITextField *pTextField in textFieldArray)
	{
		if (textField.tag == [pTextField tag])
		{
			NSLog(@"%@", pTextField.text);  
			
			[pTextField endEditing:YES]; 
			//[pTextField removeFromSuperview]; 
			
			return;
		}
	}
	
	//activeField = nil;
}


- (void)TurnToHomePage
{
	[self setTextFieldArrayVisable:YES];
	
	//if (gameState != StateHomePage) 
	{
		gameState = StateHomePage;
		
		HomePage * menu = [[[HomePage alloc] init:self] autorelease];	
		ReplaceLayerAction *replaceScreen = [[[ReplaceLayerAction alloc] initWithScene:scene layer:menu replaceLayer:currentLayer] autorelease];
		replaceScreen.reverse = true;
		[scene runAction: replaceScreen];
		currentLayer = menu;
	}
}

-(void)TurnToBottleMenu
{
	[self setTextFieldArrayVisable:YES];
	
	Bottle_Menu *l = [[[Bottle_Menu alloc] init:self] autorelease];	
	ReplaceLayerAction *replaceScreen = [[[ReplaceLayerAction alloc] initWithScene:scene layer:l replaceLayer:currentLayer] autorelease];
	replaceScreen.reverse = true;
	[scene runAction: replaceScreen];
	currentLayer = l;
}

-(void)TurnToCardMenu
{
	[self setTextFieldArrayVisable:YES];
	
	Card_Menu *l = [[[Card_Menu alloc] init:self] autorelease];	
	ReplaceLayerAction *replaceScreen = [[[ReplaceLayerAction alloc] initWithScene:scene layer:l replaceLayer:currentLayer] autorelease];
	replaceScreen.reverse = true;
	[scene runAction: replaceScreen];
	currentLayer = l;
}

- (void)HomePageSelect:(NSInteger)pIndex
{
	[self TipSound];
	
	switch (pIndex) 
	{
		case EHomePageSelect_Bottle:
			{
				//[m_pBg initWithFile:@"bg_bottle.png"];
				if(m_pBg == nil)
				{
					m_pBg = [[CCSprite alloc] init];
					[m_pBg initWithFile:@"bg_bottle.png"];
					m_pBg.anchorPoint = ccp(0.0f,0.0f);
					[scene addChild:m_pBg z:0];
				}
				else 
				{
					UIImage * image = [UIImage imageNamed: @"bg_bottle.png"];
					
					CCTexture2D * win = [ [CCTexture2D alloc] initWithImage:image];
					
					[m_pBg setTexture:win];
					
					[win release];
				}

				
				//播放音乐
				[SimpleAudioEngine sharedEngine].backgroundMusicVolume = 0.8f;
				[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"bankground_bottle.mp3" loop:YES];
				
				Bottle_Menu *l = [[[Bottle_Menu alloc] init:self] autorelease];	
				ReplaceLayerAction *replaceScreen = [[[ReplaceLayerAction alloc] initWithScene:scene layer:l replaceLayer:currentLayer] autorelease];
				[scene runAction: replaceScreen];
				currentLayer = l;
			}
			break;
		case EHomePageSelect_Cup:
			{
			
			}
			break;
		case EHomePageSelect_Cards:
			{
				//[m_pBg initWithFile:@"bg_card.png"];
				if(m_pBg == nil)
				{
					m_pBg = [[CCSprite alloc] init];
					[m_pBg initWithFile:@"bg_card.png"];
					m_pBg.anchorPoint = ccp(0.0f,0.0f);
					[scene addChild:m_pBg z:0];
				}
				else
				{
					UIImage * image = [UIImage imageNamed: @"bg_card.png"];
					
					CCTexture2D * win = [ [CCTexture2D alloc] initWithImage:image];
					
					[m_pBg setTexture:win];
					
					[win release];
				}

				
				//播放音乐
				[SimpleAudioEngine sharedEngine].backgroundMusicVolume = 0.1f;
				[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"bankground_card.mp3" loop:YES];
				
				Card_Menu *l = [[[Card_Menu alloc] init:self] autorelease];	
				ReplaceLayerAction *replaceScreen = [[[ReplaceLayerAction alloc] initWithScene:scene layer:l replaceLayer:currentLayer] autorelease];
				[scene runAction: replaceScreen];
				currentLayer = l;
			}
			break;
		default:
			break;
	}
}

#pragma mark Bottle

- (void)Bottle_MenuSelect:(NSInteger)pIndex
{
	[self TipSound];
	
	switch (pIndex) 
	{
		case EBottle_Menu_Play:
			{
				Bottle_PlayerSelected *l = [[[Bottle_PlayerSelected alloc] init:self] autorelease];	
				ReplaceLayerAction *replaceScreen = [[[ReplaceLayerAction alloc] initWithScene:scene layer:l replaceLayer:currentLayer] autorelease];
				[scene runAction: replaceScreen];
				currentLayer = l;
			}
			break;
		case EBottle_Menu_Des:
			{
				/*
				Bottle_Des *l = [[[Bottle_Des alloc] init:self] autorelease];	
				ReplaceLayerAction *replaceScreen = [[[ReplaceLayerAction alloc] initWithScene:scene layer:l replaceLayer:currentLayer] autorelease];
				[scene runAction: replaceScreen];
				currentLayer = l;
				*/
				
				Bottle_Des *l = [[[Bottle_Des alloc] init:self] autorelease];
				l.m_nReturn = EBottle_Des_Menu;
				[[CCDirector sharedDirector] pushScene:l];
				
			}
			break;
		case EBottle_Menu_Return:
			{
				[self TurnToHomePage];
			}
			break;
		default:
			break;
	}
}

- (void)Bottle_GameSelect:(NSInteger)pIndex
{
	[self TipSound];
	
	switch (pIndex) 
	{
		case EBottle_Game_Result:
			{
				Bottle_GameResult *l = [[[Bottle_GameResult alloc] init:self] autorelease];	
				ReplaceLayerAction *replaceScreen = [[[ReplaceLayerAction alloc] initWithScene:scene layer:l replaceLayer:currentLayer] autorelease];
				[scene runAction: replaceScreen];
				currentLayer = l;
			}
			break;
		case EBottle_Game_Return:
			{
				Bottle_Menu *l = [[[Bottle_Menu alloc] init:self] autorelease];	
				ReplaceLayerAction *replaceScreen = [[[ReplaceLayerAction alloc] initWithScene:scene layer:l replaceLayer:currentLayer] autorelease];
				replaceScreen.reverse = true;
				[scene runAction: replaceScreen];
				currentLayer = l;
			}
			break;
		case EBottle_Game_Des:
			{
				Bottle_Des *l = [[[Bottle_Des alloc] init:self] autorelease];
				l.m_nReturn = EBottle_Des_Menu;
				[[CCDirector sharedDirector] pushScene:l];
			}
			break;
			
		default:
			break;
	}
}

- (void)Bottle_PlayerSelected:(NSInteger)pIndex PlayerNum:(NSInteger)pPlayerNum
{
	[self TipSound];
	
	switch (pIndex)
	{
		case EBottle_PlayerSelected_Play:
		{
			Bottle_Game *l = [[[Bottle_Game alloc] init:self] autorelease];
			//l.nTotalPlayer = pPlayerNum;
			ReplaceLayerAction *replaceScreen = [[[ReplaceLayerAction alloc] initWithScene:scene layer:l replaceLayer:currentLayer] autorelease];
			[scene runAction: replaceScreen];
			currentLayer = l;
		}
			break;
		case EBottle_PlayerSelected_Return:
		{
			[self TurnToBottleMenu];
		}
			break;
		case EBottle_PlayerSelected_Des:
		{
			Bottle_Des *l = [[[Bottle_Des alloc] init:self] autorelease];
			l.m_nReturn = EBottle_Des_PlayerSelected;
			[[CCDirector sharedDirector] pushScene:l];
		}
			break;
		default:
			break;
	}
}

- (void)Bottle_DesSelect:(NSInteger)pIndex
{
	[self TipSound];
	
	[[CCDirector sharedDirector] popScene];
		
	/*
	Bottle_Menu *l = [[[Bottle_Menu alloc] init:self] autorelease];	
	ReplaceLayerAction *replaceScreen = [[[ReplaceLayerAction alloc] initWithScene:scene layer:l replaceLayer:currentLayer] autorelease];
	replaceScreen.reverse = true;
	[scene runAction: replaceScreen];
	currentLayer = l;
	*/
}

- (void)Bottle_GameResultSelected:(NSInteger)pIndex
{
	[self TipSound];
	
	switch (pIndex)
	{
		case EBottle_GameResult_Again:
		{
			Bottle_Game *l = [[[Bottle_Game alloc] init:self] autorelease];	
			ReplaceLayerAction *replaceScreen = [[[ReplaceLayerAction alloc] initWithScene:scene layer:l replaceLayer:currentLayer] autorelease];
			[scene runAction: replaceScreen];
			currentLayer = l;
		}
			break;
		case EBottle_GameResult_Return:
		{
			[self TurnToBottleMenu];
		}
			break;
		case EBottle_GameResult_Des:
		{
			Bottle_Des *l = [[[Bottle_Des alloc] init:self] autorelease];
			l.m_nReturn = EBottle_Des_PlayerSelected;
			[[CCDirector sharedDirector] pushScene:l];
		}
			break;
		default:
			break;
	}
}

#pragma mark Card
- (void)Card_MenuSelect:(NSInteger)pIndex
{
	[self TipSound];
	
	switch (pIndex) 
	{
		case ECard_Menu_Play:
		{
			Card_PlayerSelected *l = [[[Card_PlayerSelected alloc] init:self] autorelease];	
			ReplaceLayerAction *replaceScreen = [[[ReplaceLayerAction alloc] initWithScene:scene layer:l replaceLayer:currentLayer] autorelease];
			[scene runAction: replaceScreen];
			currentLayer = l;
		}
			break;
		case ECard_Menu_Des:
		{
			/*
			Card_Des *l = [[[Card_Des alloc] init:self] autorelease];	
			ReplaceLayerAction *replaceScreen = [[[ReplaceLayerAction alloc] initWithScene:scene layer:l replaceLayer:currentLayer] autorelease];
			[scene runAction: replaceScreen];
			currentLayer = l;
			*/
			
			Card_Des *l = [[[Card_Des alloc] init:self] autorelease];
			l.m_nReturn = ECard_Des_Menu;
			[[CCDirector sharedDirector] pushScene:l];
			
		}
			break;
		case ECard_Menu_Return:
		{
			[self TurnToHomePage];
		}
			break;
		default:
			break;
	}
}

- (void)Card_PlayerSelected:(NSInteger)pIndex PlayerNum:(NSInteger)pPlayerNum;
{
	[self TipSound];
	
	switch (pIndex)
	{
		case ECard_PlayerSelected_Play:
		{
			Card_Game *l = [[[Card_Game alloc] init:self] autorelease];
			l.nTotalPlayer = pPlayerNum;
			ReplaceLayerAction *replaceScreen = [[[ReplaceLayerAction alloc] initWithScene:scene layer:l replaceLayer:currentLayer] autorelease];
			[scene runAction: replaceScreen];
			currentLayer = l;
		}
			break;
		case ECard_PlayerSelected_Return:
		{
			[self TurnToCardMenu];
		}
			break;
		case ECard_PlayerSelected_Des:
		{
			Card_Des *l = [[[Card_Des alloc] init:self] autorelease];
			l.m_nReturn = ECard_Des_PlayerSelected;
			[[CCDirector sharedDirector] pushScene:l];
		}
			break;
		default:
			break;
	}
}

- (void)Card_DesSelect:(NSInteger)pIndex
{
	[self TipSound];
	
	switch (pIndex) 
	{
		case ECard_Des_Menu:
		{
			[[CCDirector sharedDirector] popScene];
		}
			break;
		case ECard_Des_PlayerSelected:
		{
			[[CCDirector sharedDirector] popScene];
			
			[self setTextFieldArrayVisable:NO];
		}
			break;
		case ECard_Des_Game:
		{
			[[CCDirector sharedDirector] popScene];
		}
			break;
		case ECard_Des_GameResult:
		{
			[[CCDirector sharedDirector] popScene];
		}
			break;
		default:
			break;
	}
	
	/*
	Card_Menu *l = [[[Card_Menu alloc] init:self] autorelease];	
	ReplaceLayerAction *replaceScreen = [[[ReplaceLayerAction alloc] initWithScene:scene layer:l replaceLayer:currentLayer] autorelease];
	replaceScreen.reverse = true;
	[scene runAction: replaceScreen];
	currentLayer = l;
	*/
}

- (void)Card_GameSelected:(NSInteger)pIndex
{
	[self TipSound];
	
	switch (pIndex)
	{
		case ECard_Game_Next:
			{
				Card_GameResult *l = [[[Card_GameResult alloc] init:self] autorelease];	
				ReplaceLayerAction *replaceScreen = [[[ReplaceLayerAction alloc] initWithScene:scene layer:l replaceLayer:currentLayer] autorelease];
				[scene runAction: replaceScreen];
				currentLayer = l;
			}
			break;
		case ECard_Game_Return:
			{
				[self TurnToCardMenu];
			}
			break;
		case ECard_Game_Des:
		{
			Card_Des *l = [[[Card_Des alloc] init:self] autorelease];
			l.m_nReturn = ECard_Des_Game;
			[[CCDirector sharedDirector] pushScene:l];
		}
			break;
		default:
			break;
	}
}

- (void)Card_GameResultSelected:(NSInteger)pIndex
{
	[self TipSound];
	
	switch (pIndex)
	{
		case ECard_GameResult_Again:
			{
				Card_PlayerSelected *l = [[[Card_PlayerSelected alloc] init:self] autorelease];	
				ReplaceLayerAction *replaceScreen = [[[ReplaceLayerAction alloc] initWithScene:scene layer:l replaceLayer:currentLayer] autorelease];
				[scene runAction: replaceScreen];
				currentLayer = l;
			}
			break;
		case ECard_GameResult_Return:
			{
				[self TurnToCardMenu];
			}
			break;
		case ECard_GameResult_Des:
		{
			Card_Des *l = [[[Card_Des alloc] init:self] autorelease];
			l.m_nReturn = ECard_Des_GameResult;
			[[CCDirector sharedDirector] pushScene:l];
		}
			break;
		default:
			break;
	}
}

-(void)TipSound
{
	[[SimpleAudioEngine sharedEngine] playEffect:@"tip.caf"];
}

@end
